<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="new :: common_head(能源属性)">
</head>
<style>
    .mylabel{
        display: inline;
    }
    .greenColor{
        color:green;
    }
    .redColor{
        color:red;
    }
</style>
<body>
<!--删除提示模态框-->
<div class="modal fade" id="delcfmModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">提示</h4>
            </div>
            <div class="modal-body">是否要删除？</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="deleteProperty()">确定</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>
<!--删除提示模态框结束-->
<!-- start: Header -->
<div th:replace="~{new::header}"></div>
<!-- start: Header -->

<div class="container-fluid-full">
    <div class="row-fluid">

        <!-- start: Main Menu -->
        <div id="sidebar-left" class="span2" th:replace="new::sidebar_left"></div>
        <!-- end: Main Menu -->

        <noscript>
            <div class="alert alert-block span10">
                <h4 class="alert-heading">Warning!</h4>
                <p>You need to have <a href="" target="_blank">JavaScript</a> enabled to use this site.</p>
            </div>
        </noscript>

        <!-- start: Content -->
        <div id="content" class="span10">


            <ul class="breadcrumb">
                <li>
                    <i class="icon-home"></i>
                    <a th:href="@{/new.html}">主页</a>
                    <i class="icon-angle-right"></i>
                </li>
                <li><a href="#">能源属性</a></li>
            </ul>

            <div class="row-fluid sortable">
                <div class="box span12">
                    <div class="box-header" data-original-title>
                        <h2><i class="halflings-icon white edit"></i><span class="break"></span>添加能源属性</h2>
                    </div>
                    <div class="box-content">
                        <!--<form class="form-horizontal"  th:action="@{/device/addDevice}" method="post">-->
                        <form id="form1" class="form-horizontal" enctype="multipart/form-data"  method="post">
                            <input type="hidden" id="typeId" name="type_id" >
                            <fieldset>
                                <div class="control-group">
                                    <label class="control-label">能源属性名称</label>
                                    <div class="controls">
                                        <input class="input-xlarge focused" type="text" placeholder=""
                                               name="typeName" onchange="checkPropertyName()" required="required" id="typeName">
                                        <div style="display: inline" id="checkName">
                                        </div>
                                        <span style="color:red;font-size: 20px" >*</span>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">能源属性图片</label>
                                    <div class="controls">
                                        <input class="input-xlarge focused"  type="file" id="icon" accept="image/jpg,image/jpeg,image/png,image/PNG" placeholder=""
                                               name="icon" >
                                    </div>
                                </div>
                                <div class="form-actions">
                                    <button type="submit" class="btn btn-primary" onclick="toAddProperty();return false">提交</button>
                                    <button  type="button" class="btn btn-primary" onclick="toDevice()">返回</button>
                                </div>
                            </fieldset>
                        </form>

                    </div>
                </div><!--/span-->

            </div><!--/row-->
        </div>
    </div>
</div>
<div class="clearfix"></div>
<div th:replace="new::common_js"></div>
<script>
    var addCheckName=true;
    function toAddProperty(){
        var formData=new FormData($("#form1")[0]);
        var typeName=document.getElementById("typeName");
        var fileInput = $('#icon').get(0).files[0];
        if(!fileInput){
            layer.alert('属性图片不能为空', {

                icon: 5,

                title: "提示"

            });
            return false;
        }
        if(typeName.value==""){
            layer.alert('属性名不能为空', {

                icon: 5,

                title: "提示"

            });return false;
        }
        if(addCheckName==false){
            layer.alert('请输入未被使用的属性名', {

                icon: 5,

                title: "提示"

            });return false;
        }
        $.ajax({
            type: "POST",
            data:formData,
            url: "/power/energyType/addEnergyType",
            processData:false,
            contentType:false,
            success: function(data) {
                console.log("成功");
                window.location.href = "/power/energyType/viewEnergyType";

            },
        })
    }
    function checkPropertyName(){
        var typeName=$("#typeName").val();
        console.log(typeName);
        $.ajax({
            type: "GET",
            data:{energyTypeName:typeName},
            url: "/power/energyType/check",
            success: function(data) {
                console.log(data.check);
                if(data.check=="yes"){
                    var span=document.createElement("span");
                    span.setAttribute("class","greenColor");
                    span.innerHTML="该属性名可以使用";
                    $("#checkName").html(span);
                    addCheckName=true;
                }
                if(data.check=="no"){
                    var span=document.createElement("span");
                    span.setAttribute("class","redColor");
                    span.innerHTML="该属性名已存在，请使用其他设备名";
                    $("#checkName").html(span);
                    addCheckName=false;
                }
            },
        })
    }
</script>
</body>
</html>
